<?php

declare(strict_types=1);

namespace App\Controller\AdminApi;

use Dotenv\Validator;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Exception\XZXValidationException;
use App\Service\ChannelInfoService;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Psr\Container\ContainerInterface;
use Hyperf\Logger\LoggerFactory;
use App\Middleware\AdminJWTAuthMiddleware;

/**
 * Class ChannelInfoController
 *
 * @package App\Controller
 * @Controller(prefix="/admin/api/channelInfo")
 * @Middleware(AdminJWTAuthMiddleware::class)
 */
class ChannelInfoController extends AdminBaseController {

    /**
     * @Inject()
     * @var ChannelInfoService
     */
    protected $ChannelInfoService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct (LoggerFactory $loggerFactory) {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    /**
     * 详情
     * @PostMapping(path = "get")
     */
    public function get (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'id' => 'required',
        ]);

        $param = [
            'id' => $request->post('id'),
        ];

        return $this->ChannelInfoService->get($param);
    }

    /**
     * 过滤添加/编辑参数
     *
     * @param RequestInterface $request
     *
     * @return array
     */
    protected function _filterEditParam (RequestInterface $request) {

        $param = [
            'channelName'      => $request->post('channelName'),
            'bankId'           => $request->post('bankId'),
            'payType'          => $request->post('payType'),
            'canPay'           => (bool) $request->post('canPay'),
            'canRefund'        => (bool) $request->post('canRefund'),
            'canTransfer'      => (bool) $request->post('canTransfer'),
            'settleCurrency'   => $request->post('settleCurrency'),
            'riskCollectionId' => $request->post('riskCollectionId'),
            'channelType'      => $request->post('channelType'),
            'params'           => $request->post('params'),
            'remark'           => $request->post('remark'),
            'status'           => $request->post('status'),
        ];

        return $param;
    }

    /**
     * 添加
     * @PostMapping(path = "add")
     *
     * @param RequestInterface $request
     *
     * @return array
     * @throws \Throwable
     */
    public function add (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'channelName'    => 'required',
            'bankId'         => 'required',
            'payType'        => 'required',
            'settleCurrency' => 'required',
            // 'riskCollectionId' => 'required',
            // 'channelType'    => 'required',
            // 'remark'         => 'required',
            'params'         => 'required',
            'status'         => 'required',
        ]);

        $param = $this->_filterEditParam($request);

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->ChannelInfoService->add($param);
    }

    /**
     * 编辑
     * @PostMapping(path = "edit")
     */
    public function edit (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'id' => 'required',

            'channelName'    => 'required',
            'bankId'         => 'required',
            'payType'        => 'required',
            'settleCurrency' => 'required',
            // 'riskCollectionId' => 'required',
            // 'channelType'    => 'required',
            // 'remark'         => 'required',
            'params'         => 'required',
            'status'         => 'required',
        ]);

        $param = $this->_filterEditParam($request);

        $param['id'] = $request->post('id');

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->ChannelInfoService->edit($param);
    }

    /**
     * 删除
     * @PostMapping(path = "del")
     */
    public function del (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'id' => 'required',
        ]);

        $param = [
            'id' => $request->post('id'),
        ];

        return $this->ChannelInfoService->del($param);
    }

    /**
     * 设置通道限额
     * @PostMapping(path = "setTradeLimit")
     */
    public function setTradeLimit (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'id' => 'required',

            'amountMin'            => 'required|integer|max:99999999900|min:0', // 单笔最小金额
            'amountLimit'          => 'required|integer|max:99999999900|min:1', // 单笔最大金额(单笔金额限额)
            'dayAmountLimit'       => 'required|integer|max:99999999900|min:1', // 每日金额限额
            'dayAmountThreshold'   => 'required|integer|min:1|max:100', // 每日金额限额阈值
            'dayCountLimit'        => 'required|integer|min:1', // 每日次数限额
            'monthAmountLimit'     => 'required|integer|max:99999999900|min:1', // 每月金额限额
            'monthAmountThreshold' => 'required|integer|min:1|max:100', // 每月金额限额阈值
            'monthCountLimit'      => 'required|integer|min:1', // 每月次数限额
        ]);

        $param = $request->post();

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->ChannelInfoService->setTradeLimit($param);
    }

    /**
     * 查看通道限额使用情况
     * @PostMapping(path = "getTradeLimitCost")
     */
    public function getTradeLimitCost (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'channelNo' => 'required', // 通道编号
        ]);

        $param = [
            'channelNo' => $request->post('channelNo'),
        ];

        $ext = $this->request->getAttribute("ext");

        $param['createBy'] = $ext['user']['username'];
        $param['updateBy'] = $ext['user']['username'];

        return $this->ChannelInfoService->getTradeLimitCost($param);
    }


    /**
     * 列表
     * @PostMapping(path = "list")
     */
    public function list (RequestInterface $request) {

        $this->_validOrFail($request->all(), [
            'page'  => 'required|integer|min:1',
            'limit' => 'required|integer|min:1',
        ]);

        $param = [
            'page'  => $request->post('page'),
            'limit' => $request->post('limit'),
        ];

        $search = $request->post('search');

        CustomUtils::xzxAddSearch($param, $search, 'channelNo');
        CustomUtils::xzxAddSearch($param, $search, 'channelName');

        return $this->ChannelInfoService->list($param);
    }

    /**
     * 列表-下拉框
     * @PostMapping(path = "options")
     */
    public function options (RequestInterface $request) {


        $param = [
            'search'  => $request->post('search', ''),
            'payType' => $request->post('payType', ''),
            'canPay' => $request->post('canPay'),
            'canRefund' => $request->post('canRefund'),
            'canTransfer' => $request->post('canTransfer'),            
        ];

        return $this->ChannelInfoService->options($param);
    }

    /**
     * 支付方式-下拉框
     * @PostMapping(path = "payTypeOptions")
     */
    public function payTypeOptions (RequestInterface $request) {

        $param = [];

        return $this->ChannelInfoService->payTypeOptions($param);
    }

    /**
     * 列表/变更日志
     * @PostMapping(path = "getInfoLog")
     */
    public function getInfoLog (RequestInterface $request) {

        $param = [
            'page'  => $request->post('page', 1),
            'limit' => $request->post('limit', 20),
        ];

        $search = $request->post('search', []);

        CustomUtils::xzxAddSearch($param, $search, 'channelId');

        return $this->ChannelInfoService->getInfoLog($param);
    }

}
